package com.visionet.school.app.web.repository.member.impl;

import com.visionet.school.app.web.common.utils.PageUtils;
import com.visionet.school.app.web.entity.member.vo.MemberClassVo;
import com.visionet.school.app.web.entity.vo.MemberVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hxpeng on 2017/4/5.
 */
public class MemberClassRepositoryImpl {

    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询所有学生根据班级Id
     * @param memberClassVo
     * @return
     */
    public Page<MemberVo> findStudentByClassId(MemberClassVo memberClassVo){

        StringBuffer sql = new StringBuffer();
        List<Object> params = new ArrayList<>();
        sql.append("select tm.id,tmc.id as tmc_id ,tm.member_name,tm.member_phone,tm.member_sex,tm.avatar_path,tm.student_number,tm.member_address,tm.member_birthday,");
        sql.append("tm.member_type from t_member_class as tmc LEFT JOIN t_member as tm on tmc.member_id = tm.id ");
        sql.append("where tmc.is_deleted = '0' and tm.is_deleted = '0' and tmc.member_type = '3' and tmc.class_id = ? ");
        params.add(memberClassVo.getClassId());
        PageRequest page = PageUtils.getPageRequestInfo(memberClassVo.getPageInfo());
        if(page == null){
            page = new PageRequest(0, 10);
        }
        int currentPage = page.getPageNumber();
        int pageSize = page.getPageSize();
        int size = jdbcTemplate.queryForInt("select count(*) from ("+sql.toString()+") as c", params.toArray());
        List<MemberVo> list = jdbcTemplate.query("select t.* from ("+sql+") as t limit  "+ currentPage * pageSize+","+pageSize, params.toArray(), new RowMapper<MemberVo>() {
            @Override
            public MemberVo mapRow(ResultSet rs, int rowNum) throws SQLException {
                MemberVo entity = new MemberVo();
                entity.setId(rs.getString("id"));
                entity.setMemberName(rs.getString("member_name"));
                entity.setMemberPhone(rs.getString("member_phone"));
                entity.setMemberSex(rs.getString("member_sex"));
                entity.setAvatarPath(rs.getString("avatar_path"));
                entity.setMemberAddress(rs.getString("member_address"));
                entity.setStudentNumber(rs.getString("student_number"));
                entity.setMemberBirthday(rs.getTimestamp("member_birthday"));
                entity.setMemberType(rs.getString("member_type"));
                entity.setMemberClassId(rs.getString("tmc_id"));
                return entity;
            }
        });
        Page<MemberVo> cmProductGroupTourPage = new PageImpl<>(list, page, size);
        return cmProductGroupTourPage;
    }


}
